// GGRRAARRGGHHH!!! PageVisibility API sucks!
(function($, a, b) {
 var $window = $(a),
  $document = $(b),
  $body = $(b.body),
  $command = $('#windows-command-proceccor'),
  $btns = $('.windows-command-top-btn'),
  $container = $('#windows-command-content-inner'),
  $contents = $container.children(),
  $thumb = $('.windows-command-scrollbar-thumb'),
  caret = b.getElementById('fake-caret'),
  $text_1 = $('.text-1'),
  $layer_1 = $('.layer-1'),
  $layer_2 = $('.layer-2'),
  $arrow = {
   up: $('.windows-command-scrollbar-btn-top'),
   down: $('.windows-command-scrollbar-btn-bot')
  }, i = 0,
  $jsp, typing = null,
  waiting = null,
  ttp = null,
  blink = setInterval(function() {
   caret.className = caret.className != 'fake-caret invisible-caret' ? 'fake-caret invisible-caret' : 'fake-caret';
  }, 700),
  str_1 = $text_1.html().replace(/^/, " ").replace(/ (.*?) (.*?) (.*?) (.*?)/g, "  $1   $2 $3 $4 ").split(''),
  str_2 = "<sp"+"an><b"+"r>'Ready' is not recognized as an internal or external command,<b"+"r>operable program or batch file.<b"+"r><b"+"r>C:\\WINDOWS\\system32></sp"+"an>",
  str_3 = "Shutdown /s /c \'Please return to the front page.\' /t 10".split('');
 $text_1.html('').removeClass('visually-hidden').before('<sp'+'an>Microsoft Windows [Version 6.1.7601]<b'+'r>Copyright (c) 2009 Microsoft Corporation.  All rights reserved.<b'+'r><b'+'r>C:\\WINDOWS\\system32></sp'+'an>');
 $window.on("resize", function() {
  $command.css({
   'top': $window.height() / 2,
   'left': $window.width() / 2
  });
 }).trigger("resize");
 $btns.hover(function() {
  ttp = setTimeout(function() {
   $btns.addClass('show-tooltip');
  }, 600);
 }, function() {
  clearTimeout(ttp);
  $(this).removeClass('show-tooltip');
 });
 $command.draggable({
  handle: '.windows-command-dragging-handle'
 });
 $container.parent().css('padding-right', 0).children().jScrollPane({
  showArrows: true,
  verticalDragMaxHeight: 24
 });
 $jsp = {
  bar: $('.jspVerticalBar'),
  thumb: $('.jspDrag'),
  arrow: {
   up: $('.jspArrowUp'),
   down: $('.jspArrowDown')
  }
 };
 $container.on("jsp-scroll-y", function() {
  $thumb.css('top', parseInt($jsp.thumb.css('top'), 10) + 18);
 });
 $jsp.bar.on("mouseenter mouseleave", function(e) {
   $thumb.parent()[e.type == "mouseenter" ? "addClass" : "removeClass"]('hovered');
 }).css('opacity', 0);
 $jsp.thumb.on("mouseenter mouseleave", function(e) {
  $thumb[e.type == "mouseenter" ? "addClass" : "removeClass"]('hovered');
 }).on("mousedown mouseup", function(e) {
  $thumb[e.type == "mousedown" ? "addClass" : "removeClass"]('clicked');
 });
 $jsp.arrow.up.on("mouseenter mouseleave", function(e) {
  $arrow.up[e.type == "mouseenter" ? "addClass" : "removeClass"]('hovered');
 }).on("mousedown mouseup", function(e) {
  $arrow.up[e.type == "mousedown" ? "addClass" : "removeClass"]('clicked');
 });
 $jsp.arrow.down.on("mouseenter mouseleave", function(e) {
  $arrow.down[e.type == "mouseenter" ? "addClass" : "removeClass"]('hovered');
 }).on("mousedown mouseup", function(e) {
  $arrow.down[e.type == "mousedown" ? "addClass" : "removeClass"]('clicked');
 });
 var startTyping = function() {
  setTimeout(function() {
   clearInterval(blink);
   caret.className = "fake-caret";
   typing = setInterval(function() {
    if (i < str_1.length) {
     $text_1.html($text_1.html().replace(/ +/g, " ") + str_1[i]);
     i++;
    } else {
     clearInterval(typing);
     i = 0;
     blink = setInterval(function() {
      caret.className = caret.className != 'fake-caret invisible-caret' ? 'fake-caret invisible-caret' : 'fake-caret';
     }, 700);
     setTimeout(function() {
      $text_1.append(str_2);
      setTimeout(function() {
       clearInterval(blink);
       caret.className = "fake-caret";
       typing = setInterval(function() {
        if (i < str_3.length) {
         $text_1.html($text_1.html().replace(/ +/g, " ") + str_3[i]);
         i++;
        } else {
         clearInterval(typing);
         blink = setInterval(function() {
          caret.className = caret.className != 'fake-caret invisible-caret' ? 'fake-caret invisible-caret' : 'fake-caret';
         }, 700);
         i = 0;
         setTimeout(function() {
          $layer_1.show();
          setTimeout(function() {
           $command.hide();
           $layer_2.fadeIn(600);
           clearInterval(blink);
           caret.className = "fake-caret";
          }, 5000);
         }, 10000);
        }
       }, 170);
      }, 5000);
     }, 400);
    }
   }, 120);
  }, 3000);
 };
 waiting = setTimeout(startTyping, 60000);
 $window.on("load", function() {
  clearTimeout(waiting);
  startTyping();
 });
})(jQuery, window, document);